 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections.Generic;

namespace Mahjong.UI
{
    public class CardUI : MonoBehaviour
    {
        public int cardValue; // 牌的值
        public bool isSelected; // 是否被选中
        private SpriteRenderer spriteRenderer; 
        private Button button; // 用于处理牌的点击事件

        //创建组件
        void Start()
        {
            spriteRenderer = GetComponent<SpriteRenderer>();
            button = GetComponent<Button>();

            if (spriteRenderer == null)
            {
                spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
            }

            if (button == null)
            {
                button = gameObject.AddComponent<Button>();
            }

            button.onClick.AddListener(OnCardClicked);
        }

        // 初始化牌
        public void Initialize(int value)
        {
            cardValue = value;
            isSelected = false;
            /*通过接口调用playerUI中的UpdateHandScene;*/
        }

         // 获取牌的精灵
        private Sprite GetCardSprite(int value)
        {
            // 牌的背面固定为 down_back_face_2.png
            if (value == 0)
            {
                return Resources.Load<Sprite>("C:/mahjong_layout/Assets/Resources/ui/card/face/down_back_face_2.png");
            }

            // 根据牌的值确定牌的类型和序号
            string cardType;
            int cardNumber;

            if (value >= 1 && value <= 9)
            {
                cardType = "万";
                cardNumber = value;
            }
            else if (value >= 11 && value <= 19)
            {
                cardType = "条";
                cardNumber = value - 10;
            }
            else if (value >= 21 && value <= 29)
            {
                cardType = "筒";
                cardNumber = value - 20;
            }
            else
            {
                // 未知牌类型，返回 null
                return null;
            }

            // 尝试加载正面牌
            string upPath = $"C:/mahjong_layout/Assets/Resources/ui/card/up/{cardType}_{cardNumber}.png";
            Sprite upSprite = Resources.Load<Sprite>(upPath);

            if (upSprite != null)
            {
                return upSprite;
            }

            // 如果正面牌不存在，尝试加载背面牌
            string downPath = $"C:/mahjong_layout/Assets/Resources/ui/card/down/{cardType}_{cardNumber}.png";
            Sprite downSprite = Resources.Load<Sprite>(downPath);

            if (downSprite != null)
            {
                return downSprite;
            }

            // 如果正面和背面牌都不存在，返回 null
            return null;
        }

        // 牌被点击时的回调
        private void OnCardClicked()
        {
            Debug.Log("Card clicked: " + cardValue);
            // 在这里可以添加牌被点击时的逻辑，比如通知 Manager 处理玩家操作
        }

        // 获取牌的值
        public int GetCardValue()
        {
            return cardValue;
        }

        // 设置牌的选中状态
        public void SetSelected(bool selected)
        {
            isSelected = selected;
            /*通过接口调用playerUI中的UpdateHandScene;*/
        }
    }
}


